<template>
	<view>
		<address-list :addressList="addressInfoList" @handleClick="handleClick"/>
		<view>
			<text @click="goToAddress" style="font-size: 80rpx;right: 40rpx;bottom: 100rpx;" class="text-primary iconfont icon-plus1 position-fixed"></text>
		</view>
	</view>
</template>

<script setup>
	import { ref, reactive } from "vue";
	import { onLoad, onShow, onNavigationBarButtonTap } from "@dcloudio/uni-app";
	import { getDeliveryAddress } from "@/api/address/index.js"
	import addressList from "@/components/address-list.vue"
	
	const addressInfoList = ref([])
	const isChoose = ref(false)
	
	onLoad((e)=>{
		if(e.type == "choose"){
			isChoose.value = true
		}
	})
	
	onShow(() => {
		getUserAddress();
	})
	
	const goToAddress = () => {
		uni.navigateTo({
			url:"/pages/addAddress/addAddress"
		})
	}
	
	const getUserAddress = async ()=>{
		let res = await getDeliveryAddress({})
		addressInfoList.value = res.data
	}
	
	const handleClick = (item)=>{
		if(isChoose.value){
			console.log("isChoose", isChoose);
			// 通知上一页
			uni.$emit("selectAddress",item)
			// 返回上一页
			uni.navigateBack()
		}
	}
	
	onNavigationBarButtonTap((e)=>{
		console.log("onNavigationBarButtonTap",e);
		if(e.index === 0){
			uni.navigateTo({
				url:"/pages/addAddress/addAddress"
			})
		}
	})
	
</script>

<style>

</style>
